<?php /** @noinspection ALL */

//订单管理

namespace app\admin\controller;

use think\facade\Db;

class Order {

	function getList($page=1,$pageSize=10){

		$input=input();

		$userinfo=Pubilc::check_jwt($input['token']);

		if(!$userinfo)return json(['code' => 0,'msg'=>'请重新登录']);

		$id=$userinfo->userId;

		$type=$userinfo->type;

		if(empty($id))return json(['code' => 0,'msg'=>'用户参数错误']);

		$condition = ' and 1=1';

		if (!empty($input['userinfo'])) {
			$input['userinfo']=trim($input['userinfo']);
			$condition .= " and (system_user.nick_name like '%{$input['userinfo']}%' or system_user.tel like '%{$input['userinfo']}%' or system_user.real_name like '%{$input['userinfo']}%' ) ";
		}

		if (!empty($input['orderinfo'])) {
			$input['orderinfo']=trim($input['orderinfo']);
			$condition .= " and (o.order_id like '%{$input['orderinfo']}%' ) ";
		}

		if (!empty($input['time'])) {
			$start=$input['time'][0]/1000;
			$end=$input['time'][1]/1000;
			$condition .= " and o.add_time >= $start and o.add_time <=  $end";
		}

		if($type==2){//代理
			$orderList = Db::query("select system_user.nick_name,system_user.tel,system_user.real_name,o.settlement_price,FROM_UNIXTIME(o.settlement_time,'%Y-%m-%d %H:%i:%s') as settlement_time,o.profit,o.status,o.order_id,o.direction,o.pay_money,o.buy_price,task.income,task.number,option_products.title,option_type.name as type_name,FROM_UNIXTIME(o.start_time,'%Y-%m-%d %H:%i:%s')  as start_time,FROM_UNIXTIME(o.add_time,'%Y-%m-%d %H:%i:%s') as add_time,FROM_UNIXTIME(task.end_time,'%Y-%m-%d %H:%i:%s') as end_time,task_type.cycle from option_order as o left join task on o.task_id=task.id join option_products on option_products.id=task.product_id join option_type on option_type.id=option_products.type_id join system_user on system_user.id=o.uid join task_type on task_type.id=task.task_typeid join agent on agent.id = system_user.agent_id where 1".$condition." and find_in_set('{$id}',agent.team_tag) order by o.id desc limit ".(($page-1)*$pageSize).",$pageSize");

			$total_num=Db::query("select count(o.id) as sum from option_order as o left join task on o.task_id=task.id join option_products on option_products.id=task.product_id join option_type on option_type.id=option_products.type_id join system_user on system_user.id=o.uid join task_type on task_type.id=task.task_typeid join agent on agent.id = system_user.agent_id where 1".$condition." and find_in_set('{$id}',agent.team_tag) ");

		}else{
			$orderList = Db::query("select system_user.nick_name,system_user.tel,system_user.real_name,o.settlement_price,FROM_UNIXTIME(o.settlement_time,'%Y-%m-%d %H:%i:%s') as settlement_time,o.profit,o.status,o.order_id,o.direction,o.pay_money,o.buy_price,task.income,task.number,option_products.title,option_type.name as type_name,FROM_UNIXTIME(o.start_time,'%Y-%m-%d %H:%i:%s')  as start_time,FROM_UNIXTIME(o.add_time,'%Y-%m-%d %H:%i:%s') as add_time,FROM_UNIXTIME(task.end_time,'%Y-%m-%d %H:%i:%s') as end_time,task_type.cycle from option_order as o left join task on o.task_id=task.id join option_products on option_products.id=task.product_id join option_type on option_type.id=option_products.type_id join system_user on system_user.id=o.uid join task_type on task_type.id=task.task_typeid where 1".$condition." order by o.id desc limit ".(($page-1)*$pageSize).",$pageSize");

			$total_num=Db::query("select count(o.id) as sum from option_order as o left join task on o.task_id=task.id join option_products on option_products.id=task.product_id join option_type on option_type.id=option_products.type_id join system_user on system_user.id=o.uid join task_type on task_type.id=option_products.task_type_id where 1".$condition);
		}

		return json(['code' => 1, 'data' => $orderList,'total_num'=>$total_num[0]['sum']]);
	}



}